<?php

namespace app\admin\model;

use think\facade\Request;
use think\Model;

/**
 * @mixin \think\Model
 */
class AdminMenu extends Model {
    
    /*
     * 菜单列表
     * author xfwed
     * date 2022.03.24 14:31 
     * ------------------------------------------
     * update phper  2022-03-24 07:10 将token记入缓存
    */
   static function getMenu($role_id=0){
        $list = AdminMenu::where('level',1)
                ->where('status',1)
                ->order('sort asc')
                ->select();
        foreach ($list as $k => $v){
            $parent_id = $v['id'];
            if($role_id){
                $row = AdminRoleMenu::where('role_id',$role_id)->where('menu_id',$parent_id)->find();
                if($row){
                    $list[$k]['checked'] = true;
                }  else {
                    $list[$k]['checked'] = FALSE;
                }
            }
            $list2 = AdminMenu::where('level',2)
                ->where('parent_id',$parent_id)
                ->where('status',1)
                ->order('sort asc')
                ->select()->toArray();
            foreach ($list2 as $key => $val){
                $parent_id2 = $val['id'];
                if($role_id){
                    $row2 = AdminRoleMenu::where('role_id',$role_id)->where('menu_id',$parent_id2)->find();
                    if($row2){
                        $list2[$key]['checked'] = true;
                    }  else {
                        $list2[$key]['checked'] = FALSE;
                    }
                }
                $list3 = AdminMenu::where('level',3)
                    ->where('parent_id',$parent_id2)
                    ->where('status',1)
                    ->order('sort asc')
                    ->select()->toArray();
                if($role_id){
                    foreach ($list3 as $ke=>$va){
                        $parent_id3 = $va['id'];
                        $row3 = AdminRoleMenu::where('role_id',$role_id)->where('menu_id',$parent_id3)->find();
                        if($row3){
                            $list3[$ke]['checked'] = true;
                        }  else {
                            $list3[$ke]['checked'] = FALSE;
                        }
                    }
                }
                $list2[$key]['children'] = $list3;
            }
            $list[$k]['children'] = $list2;
        }
        
        return $list;
    }
}
